# video_widget.py
from PySide6.QtWidgets import QLabel
from PySide6.QtCore import QRect, QPoint, Qt
from PySide6.QtGui import QPainter, QPen, QColor, QCursor

class VideoWidget(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMouseTracking(True)
        self.drawing = False
        self.rect = QRect()
        self.start_pos = QPoint()
        self.dragging = False
        self.drag_offset = QPoint()
        self.drag_corner = None
        self.corner_size = 10

    def mousePressEvent(self, event):
        pos = event.pos()

        # 点击已有矩形角落
        corner = self.get_corner_hit(pos)
        if corner:
            self.drag_corner = corner
            self.drawing = False
            return

        # 点击在已有框内（拖动）
        if self.rect.contains(pos):
            self.dragging = True
            self.drag_offset = pos - self.rect.topLeft()
            return

        # 其他情况为新绘制
        self.drawing = True
        self.start_pos = pos
        self.rect = QRect()

    def mouseMoveEvent(self, event):
        pos = event.pos()

        if self.drawing:
            self.rect = QRect(self.start_pos, pos).normalized()
            self.update()
        elif self.dragging:
            self.rect.moveTopLeft(pos - self.drag_offset)
            self.update()
        elif self.drag_corner:
            if self.drag_corner == 'tl':
                self.rect.setTopLeft(pos)
            elif self.drag_corner == 'tr':
                self.rect.setTopRight(pos)
            elif self.drag_corner == 'bl':
                self.rect.setBottomLeft(pos)
            elif self.drag_corner == 'br':
                self.rect.setBottomRight(pos)
            self.update()
        else:
            self.setCursor(Qt.ArrowCursor)
            if self.rect.contains(pos):
                self.setCursor(Qt.SizeAllCursor)
            elif self.get_corner_hit(pos):
                self.setCursor(Qt.SizeFDiagCursor)

    def mouseReleaseEvent(self, event):
        self.drawing = False
        self.dragging = False
        self.drag_corner = None

    def get_corner_hit(self, pos):
        """检测鼠标是否命中四个角"""
        if self.rect.isNull():
            return None
        corners = {
            'tl': self.rect.topLeft(),
            'tr': self.rect.topRight(),
            'bl': self.rect.bottomLeft(),
            'br': self.rect.bottomRight(),
        }
        for key, point in corners.items():
            if (pos - point).manhattanLength() <= self.corner_size:
                return key
        return None

    def paintEvent(self, event):
        super().paintEvent(event)
        if not self.rect.isNull():
            painter = QPainter(self)
            pen = QPen(QColor(255, 165, 0), 2)
            painter.setPen(pen)
            painter.drawRect(self.rect)

            # 画四个角点
            painter.setBrush(QColor(255, 255, 255))
            for point in [self.rect.topLeft(), self.rect.topRight(), self.rect.bottomLeft(), self.rect.bottomRight()]:
                painter.drawEllipse(point, self.corner_size // 2, self.corner_size // 2)
            painter.end()
